exports.createPrize = async (cloud, parmas) => {
    try{
        let create_sql = 'insert into el_prize (total_num, day_num, fixed_num, prize_name, probability) values (?, ?, ?, ?, ?)';
        let create_info = await cloud.dataspace.executeSql(create_sql, [parmas.total_num, parmas.day_num, parmas.fixed_num, parmas.prize_name, parmas.probability]);
        console.log('create_info--------------', create_info);

        return {
            status: "success",
            data: create_info
        }
    }catch(err){
        console.log('err----------', err);

        return {
            status: "fail",
            data: err
        }
    }
}

exports.reducePrizeNum = async (cloud, parmas) => {
    try{
        let update_sql = 'update el_prize set total_num = total_num - 1, day_num = day_num - 1 where total_num > 0 and day_num > 0';
        let update_info = await cloud.dataspace.executeSql(update_sql, []);

        return {
            status: "success",
            data: update_info
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
}

exports.updatePrizeNum = async (cloud, params) => {
    try{
        let select_sql = 'select * from el_prize';
        let select_info = await cloud.dataspace.executeSql(select_sql, []);
        
        for(let i = 0; i < select_info.length; i++){
            let day_num = select_info[i].total_num > 0 ? (select_info[i].total_num > select_info[i].fixed_num ? select_info[i].fixed_num : select_info[i].total_num) : 0;
            let update_sql = 'update el_prize set day_num = ? where total_num >= 0 and id = ?';
            let update_info = cloud.dataspace.executeSql(update_sql, [day_num, select_info[i].id]);
        }

        return {
            status: "success",
            data: true
        }
    }catch(err){
        return {
            status: "fail",
            data: err
        }
    }
}

exports.deletePrize = async (cloud) => {
    try{
        let delete_sql = 'delete from el_prize';
        let delete_info = await cloud.dataspace.executeSql(delete_sql, []);
        console.log('delete_info--------------', delete_info);

        return {
            status: "success",
            data: delete_info
        }
    }catch(err){
        console.log('err----------', err);

        return {
            status: "fail",
            data: err
        }
    }
}
